<script setup name="ChannelEdit">
import { ref } from 'vue'
import { articleUpdateChannelService, articleAddChannelService } from '@/api/article'

const emit = defineEmits(['success'])
let dialogVisible = ref(false)
const refForm = ref()
let formModel = ref({})
const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    { pattern: /^\S{1,10}$/, message: '分类名称必须是1-10位的非空字符', trigger: 'blur' },
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '分类别名必须是1-15位的字母或数字',
      trigger: 'blur',
    },
  ],
}

const open = (row) => {
  dialogVisible.value = true
  formModel.value = { ...row }
}

const onSubmit = async () => {
  await refForm.value.validate()
  formModel.value.id
    ? await articleUpdateChannelService(formModel.value)
    : await articleAddChannelService(formModel.value)
  ElMessage.success(formModel.value.id ? '更新成功~' : '添加成功~')
  dialogVisible.value = false
  emit('success')
}

defineExpose({
  open,
})
</script>

<template>
  <el-dialog v-model="dialogVisible" :title="formModel.id ? '编辑分类' : '添加分类'" width="30%">
    <el-form
      :model="formModel"
      :rules="rules"
      label-width="100px"
      style="padding-right: 30px"
      ref="refForm"
    >
      <el-form-item prop="cate_name" label="分类名称">
        <el-input placeholder="请输入分类名称" v-model="formModel.cate_name"></el-input>
      </el-form-item>
      <el-form-item prop="cate_alias" label="分类别名">
        <el-input placeholder="请输入分类别名" v-model="formModel.cate_alias"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="onSubmit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped></style>
